﻿@{
    ViewBag.Title = "กำหนดสิทธิ์"; 
    var db = new GERPContext(true);
    var us = db.Users.Where(o => o.RecordStatus != "D").ToList();
    var rs = db.Roles.Include("Permission.MenuItem").Where(o => o.RecordStatus != "D" && o.Permission.Any(j => j.MenuItem.SystemId == "WEBGIMS")).ToList();
    var urs = db.UserRoles.Include("Role.Permission.MenuItem").Where(o => o.RecordStatus != "D").ToList();
}

<div class="row-fluid">
    <div class="table-header span12">
        <div class="span10">
            @ViewBag.Title
        </div> 
    </div>
</div>
<table id="table" class="table table-striped table-bordered table-hover" aria-describedby="table_info">
    <thead>
        <tr>
            <th>#</th>
            @foreach (var r in rs)
            {
                <th>@r.RoleId @r.RoleName</th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (var u in us)
        {
            <tr>
                <td>@(u.Employee != null ? u.FullName : "No employee of user " + u.UserId + ", " + u.Code)</td>
                @foreach (var r in rs)
                {
                    var urInR = urs.FirstOrDefault(o => o.RoleId == r.RoleId && o.UserId == u.UserId);
                    string id = "u_" + @u.UserId + "_" + r.RoleId;
                    <td> 
                        <label>
                            <input type="checkbox" name="id" value="true" @(urInR != null ? "checked": "")>
                            <span class="lbl"></span>
                        </label>
                    </td>
                }
            </tr>
        }
    </tbody>
</table>
@section scripts {

    <script type="text/javascript">
        $(function () {


            var oTable1 = $('#table').dataTable();
        });
    </script>
}